package com.xinli.business.service.impl;

import java.util.List;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinli.business.domain.TQuestionFollow;
import com.xinli.common.utils.DateUtils;
import com.xinli.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.xinli.business.mapper.LfPostFabulousMapper;
import com.xinli.business.domain.LfPostFabulous;
import com.xinli.business.service.ILfPostFabulousService;

/**
 * 帖子收藏Service业务层处理
 *
 * @author ruoyi
 * @date 2025-04-30
 */
@Service
public class LfPostFabulousServiceImpl extends ServiceImpl<LfPostFabulousMapper,LfPostFabulous>  implements ILfPostFabulousService
{
    @Autowired
    private LfPostFabulousMapper lfPostFabulousMapper;

    /**
     * 查询帖子收藏
     *
     * @param id 帖子收藏主键
     * @return 帖子收藏
     */
    @Override
    public LfPostFabulous selectLfPostFabulousById(Long id)
    {
        return lfPostFabulousMapper.selectLfPostFabulousById(id);
    }

    /**
     * 查询帖子收藏列表
     *
     * @param lfPostFabulous 帖子收藏
     * @return 帖子收藏
     */
    @Override
    public List<LfPostFabulous> selectLfPostFabulousList(LfPostFabulous lfPostFabulous)
    {
        return lfPostFabulousMapper.selectLfPostFabulousList(lfPostFabulous);
    }

    /**
     * 新增帖子收藏
     *
     * @param lfPostFabulous 帖子收藏
     * @return 结果
     */
    @Override
    public int insertLfPostFabulous(LfPostFabulous lfPostFabulous)
    {
                lfPostFabulous.setCreateTime(DateUtils.getNowDate());
            return lfPostFabulousMapper.insertLfPostFabulous(lfPostFabulous);
    }

    /**
     * 修改帖子收藏
     *
     * @param lfPostFabulous 帖子收藏
     * @return 结果
     */
    @Override
    public int updateLfPostFabulous(LfPostFabulous lfPostFabulous)
    {
        return lfPostFabulousMapper.updateLfPostFabulous(lfPostFabulous);
    }

    /**
     * 批量删除帖子收藏
     *
     * @param ids 需要删除的帖子收藏主键
     * @return 结果
     */
    @Override
    public int deleteLfPostFabulousByIds(Long[] ids)
    {
        return lfPostFabulousMapper.deleteLfPostFabulousByIds(ids);
    }

    /**
     * 删除帖子收藏信息
     *
     * @param id 帖子收藏主键
     * @return 结果
     */
    @Override
    public int deleteLfPostFabulousById(Long id)
    {
        return lfPostFabulousMapper.deleteLfPostFabulousById(id);
    }


    @Override
    public  Long  getFollowCount(Long  postId) {
        QueryWrapper<LfPostFabulous> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("post_id",postId);
        long count = this.count(queryWrapper);
        return count;
    }

    @Override
    public  Boolean  isFollow(Long  postId) {
        QueryWrapper<LfPostFabulous> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("post_id",postId);
        queryWrapper.eq("uid", SecurityUtils.getUserIdStr());
        long count = this.count(queryWrapper);
        if(count>0){
            return true;
        }else{
            return false;
        }
    }


    @Override
    public Boolean followQuestion(Long postId) {
        // 获取当前用户ID（假设从安全上下文中获取）
        String userId = SecurityUtils.getUserIdStr();
        // 检查是否已关注
        QueryWrapper<LfPostFabulous> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("post_id", postId)
                .eq("uid", userId);
        List<LfPostFabulous>   existingFollow = this.list(queryWrapper);

        if (CollectionUtil.isEmpty(existingFollow)) {
            // 未关注，新增关注记录
            LfPostFabulous newFollow = new LfPostFabulous();
            newFollow.setPostId(postId);
            newFollow.setUid(Long.valueOf(userId));
            newFollow.setCreateTime(DateUtils.getNowDate());
            this.insertLfPostFabulous(newFollow);
            return  true;
        } else {
            // 已关注，取消关注（删除记录）
            this.remove(queryWrapper);
            return  false;
        }
    }

}
